﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Forms;

using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.DataSourcesFile;

namespace ClassroomAllocationAndManagementFrame.Data
{
    class FileGeodatabaseOperator
    {
        #region 定义一个静态的变量来存储这个类 /*让其成为单个的实例化对象，在所有程序中使用时都仅有一个实例存在*/

        private static FileGeodatabaseOperator fGDBOperator;

        private FileGeodatabaseOperator() { }

        public static FileGeodatabaseOperator GetInstance()
        {
            if (fGDBOperator == null)
            {
                fGDBOperator = new FileGeodatabaseOperator();
            }

            return fGDBOperator;
        }

        #endregion
       


        private IWorkspace pGDBWorkspace;
        private ITable pTable;

        public bool InitializeGDB(string path)
        {
            IWorkspaceFactory pGDBworkspaceFactory = new FileGDBWorkspaceFactoryClass();
            pGDBWorkspace = pGDBworkspaceFactory.OpenFromFile(@path, 0);

            if (!pGDBWorkspace.Exists())
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        public bool GetTable(string tableName)
        {
            IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pGDBWorkspace;
            try
            {
                pTable = pFeatureWorkspace.OpenTable(tableName);
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }

        }

        public bool ImportData(string[] fieldName, string[] data)
        {
            if (fieldName.Length != data.Length)
            {
                Exception e = new Exception();
                MessageBox.Show("数据和值不对应！");
                throw (e);
            }


            try
            {
                IWorkspaceEdit pGDBEdit = (IWorkspaceEdit)pGDBWorkspace;
                pGDBEdit.StartEditOperation();
                IRow pRow = pTable.CreateRow();
                for (int i = 0; i < fieldName.Length; i++)
                {
                    pRow.set_Value(pRow.Fields.FindField(fieldName[i]), data[i]);

                }
                pRow.Store();
                pGDBEdit.StopEditOperation();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            return true;
        }

        public void DeleteRow(int _OID)
        {
            IRow pRow = pTable.GetRow(_OID);
            pRow.Delete();
        }

        public void DeleteField(IField pField)
        {
            pTable.DeleteField(pField);
        }

        public void Destroy()
        {
            fGDBOperator = null;
            pGDBWorkspace = null;
        }
    }
}
